package com.wswy.wyjk.ui.drawable

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan
import com.raft.framework.utils.UICompat

class CenterImageSpan(drawable: Drawable) : ImageSpan(drawable) {

    override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
        return super.getSize(paint, text, start, end, fm) + UICompat.dp2px(4)
    }

    override fun draw(
        canvas: Canvas,
        text: CharSequence?,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {

        val b = drawable
        canvas.save()

        val transY = (bottom - b.getBounds().bottom - paint.fontMetricsInt.descent)/2

        canvas.translate(x, transY.toFloat())
        b.draw(canvas)
        canvas.restore()
    }

}